import time

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.base import Base
from selenium.webdriver.common.by import By

class Home_Page(Base):
    def search_button_click(self):
        self.click(('id', 'com.nearme.gamecenter:id/tv_hint'))

    def search_box_input(self, context):
        self.clear(('id', 'com.nearme.gamecenter:id/et_search'))
        self.send_keys(('id', 'com.nearme.gamecenter:id/et_search'), context)

    def search_click(self):
        self.click(('id', 'com.nearme.gamecenter:id/tv_search_text'))

    def search_content_click(self):
        self.click(('id', 'com.nearme.gamecenter:id/appIcon'))

    def get_search_content_text(self):
        return self.get_text((By.ID, 'com.nearme.gamecenter:id/appName'))

    def get_app_name(self):
        return self.get_text((By.ID, 'com.nearme.gamecenter:id/appName'))

    def switch_click(self):
        self.click(('id', 'com.nearme.gamecenter:id/more'))

    def switch_app_name(self):
        a = self.find_elements(('xpath', '//android.widget.TextView'))
        return a[4].text

    def rank_click(self):
        self.click(('xpath', '//android.widget.TextView[@text="排行"]'))

    def install_click(self):
        self.click(('xpath', '//android.widget.ImageView[@content-desc="安装"]'))

    def download_click(self):
        self.click(('id', 'com.nearme.gamecenter:id/img_download_icon'))

    def get_download_app_name(self):
        return self.get_text(('id', 'com.nearme.gamecenter:id/tv_name'))

    def message_click(self):
        self.click(('id', 'com.nearme.gamecenter:id/my_msg'))

    def message_game_click(self):
        self.click(('xpath', '//android.widget.TextView[@text="游戏消息"]'))

    def message_notice_click(self):
        self.click(('xpath', '//android.widget.TextView[@text="通知"]'))

    def message_interact_click(self):
        self.click(('xpath', '//android.widget.TextView[@text="互动消息"]'))

    def clear_button_click(self):
        self.click(('id', 'com.nearme.gamecenter:id/msg_setting'))

    def get_toast_text(self):
        driver = self.driver
        toast = WebDriverWait(driver, 10, 0.1).until(
            EC.presence_of_element_located((By.XPATH, "//*[contains(@text,'没有可清除的未读消息')]")))
        return toast.text

    def sort_click(self):
        self.click(('xpath','//android.widget.TextView[@text="分类"]'))



    def screen_click(self):
        self.click(('id','com.nearme.gamecenter:id/filter_tv'))

    def order_click(self):
        self.click(('xpath','//android.widget.Button[@text="预约"]'))
        self.click(('id','com.nearme.gamecenter:id/filter_confirm'))

if __name__ == '__main__':
    home = Home_Page(appPackage='com.nearme.gamecenter', appActivity='.ui.activity.SplashActivity')
    # home.sort_click()
    # print(home.get_app_name())
    # home.screen_click()
    # home.order_click()
    # print(home.get_app_name())

    # home.search_button_click()
    # home.search_box_input('原神')
    # time.sleep(3)
    # home.search_click()
    # time.sleep(2)
    # print(home.get_search_content_text())
    # home.search_content_click()
    # print(home.get_app_name())
    # print(home.switch_app_name())
    # home.switch_click()
    # print(home.switch_app_name())

    # home.rank_click()
    # time.sleep(3)
    # print(home.get_app_name())
    # home.install_click()
    # home.download_click()
    # time.sleep(3)
    # print(home.get_download_app_name())
    # time.sleep(5)

    # home.message_click()
    # home.message_game_click()
    # home.message_notice_click()
    # home.message_interact_click()
    # home.clear_button_click()
    # print(home.get_toast_text())
    home.quit()
